/*
 *
 *
Copyright (c) 1998-2001 Aristar, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. The end-user documentation included with the redistribution, if any,
must include the following acknowledgment:

"This product includes software developed by Aristar, Inc. (http://www.aristar.com/)."

Alternately, this acknowledgment may appear in the software itself, if and
wherever such third-party acknowledgments normally appear.

4. The names "Dialect" and "Aristar, Inc." must not be used to endorse or
promote products derived from this software without prior written permission.
For written permission, please contact info@aristar.com.

5. Products derived from this software may not be called "Dialect", nor may
"Dialect" appear in their name, without prior written permission of Aristar, Inc.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
ARISTAR, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This software consists of voluntary contributions made by many individuals
on behalf of Aristar, Inc.  For more information on Aristar, Inc.,
please see <http://www.aristar.com/>.

Portions of this software are based upon software freely available and
written by individuals not associated with Aristar, Inc.  Copyrights are
maintained in the relevant source code files.
 *
 *
 */

#ifndef CMETA_HEADER__
#define CMETA_HEADER__

#include "DConfigGlobals.h"
#include "COutputDevice.h"

//====================================================

#define META_TEXT	1
#define META_SITEM	2
#define META_TABLE	3

class CMetaEvent {
public:
	CMetaEvent(int x, int y):m_X(x), m_Y(y){};
	virtual ~CMetaEvent(){};
	int m_X;
	int m_Y;
};

class CTextMetaEvent : public CMetaEvent {
public:
	CTextMetaEvent(int x, int y, TString& text, U32 fontStyle)
		:m_Text(text), m_FontStyle(fontStyle), CMetaEvent(x, y){};
	virtual ~CTextMetaEvent(){};
	TString m_Text;
	U32 m_FontStyle;
};

class CSItemMetaEvent : public CMetaEvent {
public:
	CSItemMetaEvent(int x, int y, TString& label, TString& data, U32 fontStyle)
		:m_Label(label), m_Data(data), m_FontStyle(fontStyle), CMetaEvent(x, y){};
	virtual ~CSItemMetaEvent(){};
	TString m_Label;
	TString m_Data;
	U32 m_FontStyle;
};

class CTableMetaEvent : public CMetaEvent {
public:
	CTableMetaEvent(int x, int y):CMetaEvent(x, y){};
	virtual ~CTableMetaEvent(){};
};

//====================================================

#ifdef __WINCE__
#define MAX_EVENT_PER_PAGE	256
#else
#define MAX_EVENT_PER_PAGE	4096
#endif

struct CMetaPage {
	CMetaPage(float height, float width, TString& fontName, int fontSize, U32 fontStyle, int fontHeight)
		:m_Height(height), m_Width(width), m_FontName(fontName), m_FontSize(fontSize),
		 m_FontStyle(fontStyle), m_FontHeight(fontHeight)
	{
		m_MaxY = m_MinY = 0;
		m_CurrentEvent = -1;
		memset(m_MetaEvents, 0, sizeof(CMetaEvent*) * MAX_EVENT_PER_PAGE);
	}

	~CMetaPage() {
		for (int idx = 0; idx < MAX_EVENT_PER_PAGE; ++idx) {
			if (m_MetaEvents[idx] != NULL) delete m_MetaEvents[idx];
			else break;
		}
	}

	void NewTextEvent(int x, int y, TString& text, U32 fontStyle) {
		if (m_CurrentEvent >= MAX_EVENT_PER_PAGE - 1) {
			InternalError(_T("Exceeded MAX_EVENT_PER_PAGE"));
		}
		else {
			m_MetaEvents[++m_CurrentEvent] = new CTextMetaEvent(x, y, text, fontStyle);
			if (y < m_MinY) m_MinY = y;
			if (y + m_FontHeight > m_MaxY) m_MaxY = y + m_FontHeight;
		}
	}

	void NewSItemEvent(int x, int y, TString& label, TString& data, U32 fontStyle) {
		if (m_CurrentEvent >= MAX_EVENT_PER_PAGE - 1) {
			InternalError(_T("Exceeded MAX_EVENT_PER_PAGE"));
		}
		else {
			m_MetaEvents[++m_CurrentEvent] = new CSItemMetaEvent(x, y, label, data, fontStyle);
		}
	}

	void NewTableEvent(int x, int y) {
		if (m_CurrentEvent >= MAX_EVENT_PER_PAGE - 1) {
			InternalError(_T("Exceeded MAX_EVENT_PER_PAGE"));
		}
		else {
			m_MetaEvents[++m_CurrentEvent] = new CTableMetaEvent(x, y);
		}
	}

	int dy(){return m_MaxY - m_MinY;}

	float m_Height;
	float m_Width;
	TString m_FontName;
	int m_FontSize;
	U32 m_FontStyle;

	int m_FontHeight;
	int m_MinY;
	int m_MaxY;

	int m_CurrentEvent;
	CMetaEvent* m_MetaEvents[MAX_EVENT_PER_PAGE];
};

//====================================================

#ifdef __WINCE__
#define	MAX_META_PAGES	32
#else
#define MAX_META_PAGES	128
#endif

struct CMetaDoc {
	CMetaDoc(TString& docName):m_DocName(docName), m_CurrentPage(-1) {
		memset(m_MetaPages, 0, sizeof(CMetaPage*) * MAX_META_PAGES);
	};
	~CMetaDoc() {
		for (int idx = 0; idx < MAX_META_PAGES; ++idx) {
			if (m_MetaPages[idx] != NULL) delete m_MetaPages[idx];
			else break;
		}
	}
	CMetaPage* NewPage(float height, float width, TString& fontName, int fontSize, U32 fontStyle, int fontHeight) {
		if (m_CurrentPage >= MAX_META_PAGES - 1) {
			InternalError(_T("Exceeded MAX_META_PAGES"));
			return NULL;
		}
		else {
			m_MetaPages[++m_CurrentPage] = new CMetaPage(height, width, fontName, fontSize, fontStyle, fontHeight);
			return m_MetaPages[m_CurrentPage];
		}
	}

	TString m_DocName;
	int m_CurrentPage;
	CMetaPage* m_MetaPages[MAX_META_PAGES];
};

//====================================================

#define MAX_META_DOCS	64

struct CMeta {
	CMeta(TString& name) {
		m_Name = name;
		m_CurrentDoc = -1;
		memset(m_MetaDocs, 0, sizeof(CMetaDoc*) * MAX_META_DOCS);
	}
	~CMeta() {
		for (int idx = 0; idx < MAX_META_DOCS; ++idx) {
			if (m_MetaDocs[idx] != NULL) delete m_MetaDocs[idx];
			else break;
		}
	};
	CMetaDoc* StartDocument(TString& docName) {
		if (m_CurrentDoc >= MAX_META_DOCS - 1) {
			InternalError(_T("Exceeded MAX_META_DOCS"));
			return NULL;
		}
		else {
			m_MetaDocs[++m_CurrentDoc] = new CMetaDoc(docName);
			return m_MetaDocs[m_CurrentDoc];
		}
	}
	void AbortDocument() {
		if (m_CurrentDoc < 0) return;
		else {
			delete m_MetaDocs[m_CurrentDoc];
			m_MetaDocs[m_CurrentDoc--] = NULL;
		}
	}

	POINT m_PixPerInch;	// Of reference device
	TString m_Name;
	int m_CurrentDoc;
	CMetaDoc* m_MetaDocs[MAX_META_DOCS];
};

//====================================================

#define PAGE_BREAK_HEIGHT	30	// pixels

class CMetaViewer {
public:
	CMetaViewer(CInterpreter* pInterp);
	~CMetaViewer();
	bool IsVisible();
	void ShowWindow(int show);
	void SetTitle(LPCTSTR title);
	void DisplayMeta(CMeta* meta);

protected:
	CInterpreter* m_pInterp;
	static VALUE m_vHwndToDev;

	HWND m_MetaWindow;
	HWND m_TextWindow;
	HWND m_DocComboBox;
	HWND m_CloseButton;
	int m_ScrollHeight;
	CMeta* m_Meta;
	LOGFONT m_fn;

	BOOL RegisterMetaWindow(HINSTANCE hInstance);
	HWND CreateMetaWindow(HINSTANCE hInstance, int nCmdShow);
	static LRESULT CALLBACK MetaWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
	static LRESULT CALLBACK TextWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
	void OnPaint(HWND hWnd, HDC hDC);
	//static void SetWindowFont(HDC hDC, TString& fontName, int fontSize, U32 fontStyle);

	void DetermineColWidths(const TTable& table, int colWidths[], int rowHeights[]);
	void DetermineRowHeights(const TTable& table, int colWidths[], int rowHeights[]);
};

//====================================================

class CMetaOutputDev : public COutputDev {
public:
	CMetaOutputDev(CInterpreter* pInterp);
	~CMetaOutputDev();
	void ShowWindow();

	void StartDocument(LPCTSTR docName);
	void EndDocument();
	void AbortDocument();
	void TextOut(LPCTSTR text);
	void _TextOut(LPCTSTR text);
	void TextOutTab();
	void TextOutCR(int lineHeight = -1);
	int LinesLeft();
	void SetFont(TString fontName, int fontSize, U32 fontStyle);
	void SetMargins(float left, float top, float right, float bottom);

	void PrintTable(const TTable& table);

	void NewPage();
	void FillTo(int column, TCHAR fillChar);
	bool bPrintingDoc;
	bool bPrintingPage;

	//void EndSection();
	//void StartSection(TString& label, TString& data, U32 style);
	//void PrintSectionHeaders();
	//void SectionItem(TString& label, TString& data, TCHAR fillChar);

	float GetX();
	float GetY();
	void SetX(float x);
	void SetY(float y);
	float TextWidth(TString text);

	CMetaViewer* m_MetaViewer;
protected:
	void DetermineColWidths(const TTable& table, int colWidths[], int rowHeights[]);
	void DetermineRowHeights(const TTable& table, int colWidths[], int rowHeights[]);
	int HowManyLines(LPCTSTR text, int width);
	void _HowManyLinesTextOut(LPCTSTR text, int width, int& currentX, int& lineCount);

	CMeta* m_Meta;
	CMetaDoc* m_MetaDoc;
	CMetaPage* m_MetaPage;

	HDC m_hDC;
	HFONT m_hFont;
	TEXTMETRIC m_TextMetrics;
	int m_LineHeight;
	int m_LeftMargin;
	POINT m_ptGutter;
	POINT m_ptPageSize;
	POINT m_ptPixPerInch;
};

//====================================================
#endif // CMETA_HEADER__
